
package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseDao<T> {

    public boolean batch(String sql, List<Object[]> list) {
        Connection conn = ConnectionDB.getConnection();
        PreparedStatement pst = null;
        try {
            conn.setAutoCommit(false);
            pst = conn.prepareStatement(sql);
            for (int i = 0; i < list.size(); i++) {
                for (int j = 0; j < list.get(i).length; j++) {
                    pst.setObject(j + 1, list.get(i)[j]);
                }

                pst.addBatch();

            }
            int row = pst.executeBatch().length;
            conn.commit();
            if (row != 0) {
                return true;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            try {

                conn.rollback();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            ConnectionDB.closeConnection(pst, conn);
        }
        return false;
    }

    public boolean update(String sql, Object[] arr) {
        int row = 0;
        Connection conn = ConnectionDB.getConnection();
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);

            for (int i = 0; i < arr.length; i++) {
                pst.setObject(i + 1, arr[i]);
            }
            row = pst.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            ConnectionDB.closeConnection(pst, conn);
        }
        if (row != 0) {
            return true;
        }
        return false;
    }

    public List<T> findMultiple(String sql, Object[] arr) {
        List<T> list = new ArrayList<T>();
        Connection conn = ConnectionDB.getConnection();
        PreparedStatement pst = null;
        T t = null;
        try {
            pst = conn.prepareStatement(sql);
            if (arr != null) {
                for (int i = 0; i < arr.length; i++) {
                    pst.setObject(i + 1, arr[i]);
                }
            }
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                t = this.rowMapper(rs);
                list.add(t);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            ConnectionDB.closeConnection(pst, conn);
        }
        return list;
    }

    public T find(String sql, Object[] arr) {
        List<T> list = new ArrayList<T>();
        Connection conn = ConnectionDB.getConnection();
        PreparedStatement pst = null;
        T t = null;
        try {
            pst = conn.prepareStatement(sql);
            for (int i = 0; i < arr.length; i++) {
                pst.setObject(i + 1, arr[i]);
            }
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                t = this.rowMapper(rs);

            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            ConnectionDB.closeConnection(pst, conn);
        }
        return t;
    }

    public abstract T rowMapper(ResultSet rs);

//	public abstract T rowMapperVo(ResultSet rs);

    public static void main(String[] args) {

    }
}